//
//  MPNewsTableCell.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/10.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPNewsTableCell.h"
#import "UIImage+Common.h"
#import "MPNewsTableItem.h"
#import "NSDate+Business.h"
#import "UIImageView+WebCache.h"

@interface MPNewsTableCell ()
@property (nonatomic, strong)UILabel* titleLabel;
@property (nonatomic, strong)UILabel* timeLabel;
@property (nonatomic, strong)UIImageView* thumbnailImageView;
@property (nonatomic, strong)UIImageView* topSeperatorView;
@property (nonatomic, strong)UIImageView* bgImageView;
@property (nonatomic, strong)UIImageView* bottomSeperatorView;

@end

@implementation MPNewsTableCell
+ (CGFloat)rowHeightForObject:(id)object
{
    return 95.0f;
}

//+ (CGFloat)estimateRowsHeight
//{
//    return 95.0f;
//}

+ (BOOL)shouldUseAutoLayout
{
    return YES;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        // TODO 去掉默认的高度为44的约束
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        
        [self.contentView addSubview:self.topSeperatorView];
        [self.contentView addSubview:self.bgImageView];
        [self.contentView addSubview:self.bottomSeperatorView];
        [self.bgImageView addSubview:self.titleLabel];
        [self.bgImageView addSubview:self.timeLabel];
        [self.bgImageView addSubview:self.thumbnailImageView];
        
        [self setupConstraints];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    self.titleLabel.text = nil;
    self.timeLabel.text = nil;
    self.thumbnailImageView.image = nil;
}

- (void)setupConstraints
{
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.trailing.mas_equalTo(self);
    }];
    
    [self.topSeperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.contentView).with.offset(10.0f);
        make.height.mas_equalTo(@(1.0f));
    }];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topSeperatorView.mas_bottom);
        make.leading.trailing.mas_equalTo(self.contentView);
    }];
    
    [self.bottomSeperatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgImageView.mas_bottom);
        make.leading.trailing.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(@1.0f);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgImageView).with.offset(15.0f);
        make.leading.mas_equalTo(self.contentView).with.offset(10.0f);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(10.0f);
        make.leading.trailing.mas_equalTo(self.titleLabel);
        make.bottom.mas_equalTo(self.bgImageView).with.offset(-15.0f);
    }];
    
    [self.thumbnailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bgImageView);
        make.trailing.mas_equalTo(self.bgImageView).with.offset(-10.0f);
        make.leading.mas_equalTo(self.titleLabel.mas_trailing).with.offset(10.0f);
        make.width.height.mas_equalTo(@45.0f);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)setObject:(id)object
{
    [super setObject:object];
    
    if (object == nil)
    {
        return ;
    }
    else
    {
        if ([object isKindOfClass:[MPNewsTableItem class]]) {
            MPNewsTableItem* item = (MPNewsTableItem *)object;
            if (![NSString stringIsEmpty:item.newsTitle]) {
                self.titleLabel.text = item.newsTitle;
            }
            
            if (item.timeStamp) {
                self.timeLabel.text = [item.timeStamp newsTableTimeString];
            }
            
            if ([NSString stringIsEmpty:item.imageUrl]) {
                [self.thumbnailImageView setImage:[UIImage imageFromColor:[UIColor redColor]]];
            }
            else
            {
                [self.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:item.imageUrl] placeholderImage:[UIImage imageFromColor:[UIColor redColor]]];
            }
        }
    }
}


#pragma mark - property
- (UIImageView *)topSeperatorView
{
    if (!_topSeperatorView) {
        _topSeperatorView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor]]];
    }
    return _topSeperatorView;
}

- (UIImageView *)bottomSeperatorView
{
    if (!_bottomSeperatorView) {
        _bottomSeperatorView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor]]];
    }
    return _bottomSeperatorView;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor whiteColor]]];
    }
    return _bgImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.textColor = [UIColor colorWithHexString:kContentTextColor];
        _titleLabel.font = [UIFont systemFontOfSize:kContentFontSize];
        _titleLabel.numberOfLines = 1;
    }
    return _titleLabel;
}

- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _timeLabel.textColor = [UIColor colorWithHexString:kTextUnSelectedGrayColor];
        _timeLabel.font = [UIFont systemFontOfSize:kDescribeFontSize];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _timeLabel;
}

- (UIImageView *)thumbnailImageView
{
    if (!_thumbnailImageView) {
        _thumbnailImageView = [[UIImageView alloc] init];
    }
    return _thumbnailImageView;
}

@end
